package com.swsc.solarverse.service.schedule;

import com.swsc.solarverse.common.util.BizAssert;
import com.swsc.solarverse.common.util.LocalDateTimeUtils;
import com.swsc.solarverse.dao.entity.sys.CorporationDO;
import com.swsc.solarverse.service.sys.ICorporationService;

import java.time.LocalDateTime;

/**
 * @Author CHENCL-X99
 * @Date 2025/5/13 8:58
 * @Version 1.0
 * @Description
 */
@lombok.extern.slf4j.Slf4j
@org.springframework.stereotype.Service
public class ScheduleService {
    private final ICorporationService corporationService;
    private final StringEfficiencyRecordAnalysisService stringEfficiencyRecordAnalysisService;
    private final StringIuAnalysisService stringIuAnalysisService;

    public ScheduleService(ICorporationService corporationService,
                           StringEfficiencyRecordAnalysisService stringEfficiencyRecordAnalysisService,
                           StringIuAnalysisService stringIuAnalysisService) {
        this.corporationService = corporationService;
        this.stringEfficiencyRecordAnalysisService = stringEfficiencyRecordAnalysisService;
        this.stringIuAnalysisService = stringIuAnalysisService;
    }

    /**
     * 组串能效计算 取9-15点数据, 每分钟一条进行积分计算
     * @param organNo
     * @param dateTimes
     */
    public void analysisStringEfficiencyRecord(Integer organNo, String dateTimes) {
        log.info("低效组串发电量计算-开始...");
        CorporationDO corporationDo = corporationService.checkStationByOrganNo(organNo);
        BizAssert.isFalse(null == corporationDo, "低效组串发电量计算-未查询到电站组织信息...");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nowDayStart = LocalDateTimeUtils.getDayStart(now);
        String[] dataTimeArr = dateTimes.split(",");
        for (String dateTimeStr : dataTimeArr) {
            LocalDateTime dateTime = LocalDateTimeUtils.str2Time(dateTimeStr, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime dataTime = dateTime.withHour(11).withMinute(10).withSecond(0).withNano(0);
            log.info("低效组串发电量计算-查询数据: {}", dataTime);
            stringEfficiencyRecordAnalysisService.analysisStationStringDataNew(corporationDo, dataTime, now);
        }
    }

    public void analysisStringIuRecord(Integer organNo, String dateTimes) {
        log.info("组串IU低效诊断分析-开始...");
        CorporationDO corporationDo = corporationService.checkStationByOrganNo(organNo);
        BizAssert.isFalse(null == corporationDo, "组串IU低效诊断分析-未查询到电站组织信息...");
        LocalDateTime now = LocalDateTime.now();
        String[] dataTimeArr = dateTimes.split(",");
        for (String dateTimeStr : dataTimeArr) {
            LocalDateTime dateTime = LocalDateTimeUtils.str2Time(dateTimeStr, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime dataTime = dateTime.withHour(11).withMinute(10).withSecond(0).withNano(0);
            log.info("组串IU低效诊断分析-具体分析逻辑: {}", dataTime);

            // 具体分析逻辑
            stringIuAnalysisService.analysisNew(corporationDo.getTenantNo(), organNo, dataTime, now);
        }
    }
}
